#include "parser.h"

Parser::Parser() {
	Nama = "";
	Jml_Param = 0;
	Jml_Rel = 0;
}

void Parser::Parse(string S) {
	int i;
	i = 0;
	string Temp;
	Temp = "";
	while (S.at(i) != '(') {
		Temp.push_back(S.at(i));
		i++;
	}
	Nama = Temp;
	Temp = "";
	i++;
	while (i < (S.length()-1)) {
		while (S.at(i) != ')') {
			Temp.push_back(S.at(i));
			i++;
			if ((S.at(i) == ',') || (S.at(i) == ')')) {
				if (S.at(i) != ')') i++;
				ListOfParam.push_back(Temp);
				Temp = "";
				Jml_Param++;
			}
		}
		//ignore ':-'
		i++;
		Temp = "";
		//selesai ignore ':-'
		IgnoreBlank(S,&i);
		while (i < S.length()) {
			if (S.at(i-1) == ',') IgnoreBlank(S,&i);
			Temp = "";
			while ((S.at(i) != ',') && (S.at(i) != '.')) {
				if (S.at(i) == ':') {
					i+=2;
					IgnoreBlank(S,&i);
				}
				if (S.at(i) != '(') {
					Temp.push_back(S.at(i));
					i++;
				} else {
					while (S.at(i) != ')') {
						Temp.push_back(S.at(i));
						i++;
					}
					Temp.push_back(S.at(i));
					i++;
					IgnoreBlank(S,&i);
				}
			}
			if ((S.at(i) == ',') || (S.at(i) == '.')) {
				ListOfRel.push_back(Temp);
				Jml_Rel++;
			}
			i++;
		}
		//if (S.at(i) == ',') i--;
	}
}

void Parser::Print() {
	cout << "NAMA : " << Nama << endl;
	list<string>::iterator i;
	int j,k;
	j = 1;
	k = 1;
	cout << "LIST OF PARAMETER :" << endl;
	for(i=ListOfParam.begin(); i != ListOfParam.end(); ++i) {
		cout << "  " << j << " : " << *i << endl;
		j++;
	}
	cout << "NUMBER OF PARAMETER : " << Jml_Param << endl;
	cout << "LIST OF RELASI :" << endl;
	for(i=ListOfRel.begin(); i != ListOfRel.end(); ++i) {
		cout << "  " << k << " : " << *i << endl;
		k++;
	}
	cout << "NUMBER OF RELASI : " << Jml_Rel << endl;
}

void Parser::IgnoreBlank(string S, int* C) {
	while (S.at(*C) == ' ') {
		(*C)++;
	}
}

void Parser::Clear() {
	Nama = "";
	Jml_Param = 0;
	Jml_Rel = 0;
	ListOfParam.clear();
	ListOfRel.clear();
}
